{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Erdos-Renyi (ER) Model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import graspologic\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Erdos-Renyi (ER) graphs are the simplest generative model. We assume that each edge for all pairs of vertices is sampled independently from all the edges. There are two ways to parameterize the model:\n",
    "\n",
    "1. ER(n, p) - this model specifies the number of vertices, $n$, and each pair of vertices has $p$ probability of an edge existing between the two.\n",
    "2. ER(n, m) - this model specifies the number of vertices, $n$, and the total number of edges $m$.\n",
    "\n",
    "Below, we sample two binary graphs (undirected and no self-loops) $G_1 \\sim ER_{NP}(50, 0.3)$ and $G_2 \\sim ER_{NM}(50, 250)$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from graspologic.simulations import er_nm, er_np\n",
    "\n",
    "n = 50\n",
    "m = 250\n",
    "p = 0.3\n",
    "\n",
    "np.random.seed(1)\n",
    "G1 = er_np(n=n, p=p)\n",
    "G2 = er_nm(n=n, m=m)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Visualize the graphs using heatmap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from graspologic.plot import heatmap\n",
    "\n",
    "heatmap(G1, title = 'ER-NP(50, 0.3) Simulation')\n",
    "_ = heatmap(G2, title = 'ER-NM(50, 250) Simulation')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Weighted ER Graphs\n",
    "\n",
    "Both ``er_np()`` and ``er_nm()`` functions provide ways to sample weights for all edges that were sampled via a probability distribution function. In order to sample with weights, we provide a probability distribution function with corresponding keyword arguments for the distribution function.\n",
    "\n",
    "Below we sample $G_1 \\sim ER_{NP}(50, 0.2)$ where the weights are distributed normally with $\\mu = 0,~\\sigma^2 = 1$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "wt = np.random.normal\n",
    "wtargs = dict(loc=0, scale=1)\n",
    "\n",
    "G1 = er_np(n=50, p=0.2, wt=wt, wtargs=wtargs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Visualize the graph using heatmap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "_ = heatmap(G1, title = 'ER-NP(50, 0.2) with N(0,1) Weights Simulation')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
